package com.house.framework.utils;

import com.house.framework.entity.TSysSsoItem;
import org.apache.log4j.Logger;

import java.util.*;

public class CacheManager {

    private static final Logger LOG = Logger.getLogger(CacheManager.class);

    //****Session过期时间
    private static String _SessionOutHour = "0.5";

    public static String get_SessionOutHour() {
        return _SessionOutHour;
    }

    public static void set_SessionOutHour(String _SessionOutHour) {
        CacheManager._SessionOutHour = _SessionOutHour;
    }

    //****登陆缓存对象 .net用的Dictionary
    private static final Map<String, TSysSsoItem> SSOContxtList = new Hashtable<>();

    /**
     * 判断令牌是否存在
     *
     * @param token 令牌
     * @return
     */
    public static boolean TokenIsExist(String token) {
        synchronized (SSOContxtList) {
            return SSOContxtList.containsKey(token);
        }
    }

    /**
     * 添加令牌
     * @param token 令牌
     * @param EncryptKey 加密key
     * @param info 凭证
     * @param timeout 过期时间
     */
    public static void TokenInsert(String token, String EncryptKey, Object info, Date timeout) {
        try {
            //****锁定资源
            synchronized (SSOContxtList) {
                //****如果不存在KEY，添加到集合中
                if (!TokenIsExist(token)) {
                    //当前时间+ 多少分钟
                    Double minute = Double.parseDouble(get_SessionOutHour()) * 60*60*1000;
                    Long time = System.currentTimeMillis()+ minute.longValue();
                    TSysSsoItem context = new TSysSsoItem(token, EncryptKey, info, new Date(time));
                    SSOContxtList.put(token, context);
                }
                //****清理过期的Token
                DeleteOutTimeToken();
            }
        } catch (Exception e) {
            LOG.info("TokenInsert----出错----" + e.toString());
        }

    }

    private static void DeleteOutTimeToken() {
        try {
            List<String> tokens = new ArrayList<>();
            for (TSysSsoItem context : SSOContxtList.values()) {
                if(compareDate(context.getLastTime(),new Date(System.currentTimeMillis()))<=0){
                    tokens.add(context.getToken());
                }
            }
            for (String token : tokens) {
                SSOContxtList.remove(token);
            }
        } catch (Exception e) {
            LOG.info("DeleteOutTimeToken-----出错-----" + e.toString());
        }
    }

    public static int compareDate(Date date, Date now) {
        return date.compareTo(now);
    }

    /**
     * 获取加密Key
     * @param token 令牌
     * @return
     */
    public static String EncryptKeyExist(String token){
        synchronized (SSOContxtList){
            if(TokenIsExist(token)){
                return SSOContxtList.get(token).getEncryptKey();
            }else {
                return "";
            }
        }
    }

    /**
     * 删除token
     * @param token 令牌
     */
    public static void DeleteToken(String token){
        try{
            synchronized (SSOContxtList){
                if(TokenIsExist(token)){
                    SSOContxtList.remove(token);
                }
            }
        }catch (Exception e){
            LOG.info("DeleteToken-----出错-----" + e.toString());
        }
    }
}
